package com.imageim.yimirror.common.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.imageim.yimirror.base.BaseApplication;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 数据缓存工具类
 */
public class CacheUtils {

    private static SharedPreferences mSharedPreferences;
    private static String CACHE_FILE_NAME = "yimirrorcache";

    public static void clear() {
        getPreference(BaseApplication.getContext()).edit().clear().apply();
    }

    /**
     * 将数据保存到SharedPreferences中
     *
     * @param context
     * @param key     要保存的key值
     * @param value   要保存的boolean值
     */
    @SuppressWarnings("static-access")
    public static void setBoolean(Context context, String key, boolean value) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences(CACHE_FILE_NAME, Context.MODE_PRIVATE);
        }
        mSharedPreferences.edit().putBoolean(key, value).apply();
    }

    /**
     * 获取保存在SharedPreferences中的数据
     *
     * @param context
     * @param key     要获取的key值
     *                缺省值
     * @return 获取的value值
     */
    @SuppressWarnings("static-access")
    public static boolean getBoolean(Context context, String key) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences(CACHE_FILE_NAME, Context.MODE_PRIVATE);
        }
        return mSharedPreferences.getBoolean(key, false);
    }

    private static SharedPreferences getPreference(Context context) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences(CACHE_FILE_NAME, Context.MODE_PRIVATE);
        }
        return mSharedPreferences;
    }

    /**
     * 保存String类型的数据
     *
     * @param context
     * @param key
     * @param value
     */
    @SuppressWarnings("static-access")
    public static void setString(Context context, String key, String value) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences(CACHE_FILE_NAME, Context.MODE_PRIVATE);
        }
        mSharedPreferences.edit().putString(key, value).apply();
    }

    public static void setDouble(Context context, String key, double value) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences(CACHE_FILE_NAME, Context.MODE_PRIVATE);
        }
        mSharedPreferences.edit().putString(key, value + "").apply();
    }

    public static double getDouble(Context context, String key, double defValue) {
        String str = getPreference(context).getString(key, defValue + "");
        return Double.parseDouble(str);
    }

    public static void setLong(Context context, String key, Long value) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences(CACHE_FILE_NAME, Context.MODE_PRIVATE);
        }
        mSharedPreferences.edit().putLong(key, value).apply();
    }

    public static long getLong(Context context, String key, Long defValue) {
        return getPreference(context).getLong(key, defValue);
    }

    public static void setFloat(Context context, String key, float value) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences(CACHE_FILE_NAME, Context.MODE_PRIVATE);
        }
        mSharedPreferences.edit().putFloat(key, value).apply();
    }

    public static float getFloat(Context context, String key, float defValue) {
        return getPreference(context).getFloat(key, defValue);
    }


    /**
     * 获取String类型的数据
     *
     * @param context
     * @param key
     * @return
     */
    @SuppressWarnings("static-access")
    public static String getString(Context context, String key) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences(CACHE_FILE_NAME, Context.MODE_PRIVATE);
        }
        return mSharedPreferences.getString(key, "");
    }

    @SuppressLint("NewApi")
    public static void setStringArray(Context context, String key, List<String> values) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences(CACHE_FILE_NAME, Context.MODE_PRIVATE);
        }
        if (values == null || values.size() == 0) {
            mSharedPreferences.edit().putString(key, "").apply();
        } else {
            String str = "";
            for (int i = 0; i < values.size(); i++) {
                if (values.get(i) != null) {
                    str += values.get(i) + "#-#";
                }
            }
            String subStr = str.substring(0, str.length() - 3);
            mSharedPreferences.edit().putString(key, subStr).apply();
        }

    }

    @SuppressLint("NewApi")
    public static List<String> getStringArray(Context context, String key) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences(CACHE_FILE_NAME, Context.MODE_PRIVATE);
        }
        List<String> resultList = new ArrayList<String>();
        String str = mSharedPreferences.getString(key, "");
        if ("".equals(str)) {
            return resultList;
        }
        String[] splitStrs = str.split("#-#");
        if (splitStrs != null && splitStrs.length != 0) {
            for (int i = 0; i < splitStrs.length; i++) {
                resultList.add(splitStrs[i]);
            }
        }
        return resultList;
    }

    public static void setInt(Context context, String key, int value) {
        getPreference(context).edit().putInt(key, value).apply();
    }

    public static int getInt(Context context, String key) {
        return getPreference(context).getInt(key, 0);
    }

    public static int getInt(Context context, String key, int defValue) {
        return getPreference(context).getInt(key, defValue);
    }

    public static void setBean(Context context, String key, Object obj) {
        SharedPreferences.Editor editor = getPreference(context).edit();
        String json = new Gson().toJson(obj);
        editor.putString(key, json).apply();
    }

    public static <T> T getBeanByGson(Context context, String key,
                                      Class<T> clazz) {
        String objString = getPreference(context).getString(key, "");
        return new Gson().fromJson(objString, clazz);
    }
}
